package com.ruoyi.project.digital.interceptor;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.AuthUtil;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.interceptor.annotation.ApiRepeatSubmit;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.digital.domain.dto.LoginWechatUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

/**
 * 防止重复提交拦截器
 *
 * @author ruoyi
 */
@Component
public class WechatUserRepeatSubmitInterceptor extends HandlerInterceptorAdapter {

    private static final String PRE = "SRT_";
    private String key = null;
    @Autowired
    private RedisCache redisCache;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        boolean isRpcRequest = requestURI.startsWith("/api/wu");
        if (isRpcRequest) {
            if (handler instanceof HandlerMethod) {
                HandlerMethod handlerMethod = (HandlerMethod) handler;
                Method method = handlerMethod.getMethod();
                ApiRepeatSubmit annotation = method.getAnnotation(ApiRepeatSubmit.class);
                if (annotation != null) {
                    if (this.isRepeatSubmit(request)) {
                        AjaxResult ajaxResult = AjaxResult.error("不允许重复提交，请稍后再试");
                        ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult));
                        return false;
                    }
                }
            }
            return true;
        } else {
            return super.preHandle(request, response, handler);
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

        if (StringUtils.isNotEmpty(this.key) && null != redisCache.getCacheObject(key)) {
            redisCache.deleteObject(key);
        }
    }


    /**
     * 验证是否重复提交由子类实现具体的防重复提交的规则
     *
     * @param request
     * @return
     * @throws Exception
     */
    public boolean isRepeatSubmit(HttpServletRequest request) {
        String key = getkey(request);
        this.key = key;
        if (null != redisCache.getCacheObject(key)) {
            return true;
        } else {
            redisCache.setCacheObject(key, key, 10, TimeUnit.SECONDS);
        }
        return false;
    }


    private String getkey(HttpServletRequest request){
        LoginWechatUser member = AuthUtil.getWechatUser();
        String requestURI = request.getRequestURI();
        String key = PRE + member.getId() + requestURI;
       return SecureUtil.md5().digestHex(key);
    }



}
